<?php

namespace app\api\controller;
use app\common\controller\Api;
use think\Validate;
use think\Loader;
use think\Page;
use think\Db;
use \app\admin\model\JoinArticle as JoinArticles;
use \app\admin\model\Signup;
class Joinarticle extends Api
{ 
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $user_id = 0;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
    }

    /**
     * @MethodName 獲取加入我們文章列表
     * @Author     Allen
     * @DateTime   2021-10-04T14:55:12+0800
     * @param      string
     * @return     [array]
     */
    public function getJoinArticleList($value='')
    {
        // 信息分類1義工報名2招聘報名
        $cat_id = $this->request->post("cat_id", 1);
        if ( !in_array($cat_id, [1,2]) ) {
            $cat_id = '1';
        }
        $where = [];
        $where['cat_id'] = $cat_id;

        $collection_arr = ['1'=>'7','2'=>'8'];

        $model = new JoinArticles;
        $total = $model
                ->where($where)
                ->count();

        $page = new Page($total,10);
        $list = $model
                ->field('id,images,title,createtime')
                ->where($where)
                ->limit($page->firstRow.','.$page->listRows)
                ->order('weigh desc,id desc')
                ->select();

        foreach ($list as $key => &$value) {
            $value['collection_num'] = db('collection')
                                        ->where(['type'=>$collection_arr[$cat_id],'relation_id'=>$value['id']])
                                        ->count();
        }

        unset($value);
        $data = ['total'=>$total,'listrows'=>$page->listRows,'totalpages'=>$page->totalPages,'data'=>$list];
        $this->success('請求成功',$data);
    }

    /**
     * @MethodName 獲取加入我們詳情
     * @Author     Allen
     * @DateTime   2021-10-04T15:42:20+0800
     * @return     [type]
     */
    public function getJoinArticleInfo()
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Article');
        if (!$validate->scene('info')->check($rq_data)) {
            $this->error($validate->getError());
        }

        $collection_arr = ['1'=>'7','2'=>'8'];

        $model = new JoinArticles;
        $srmodel = new Signup;
        $info = $model
                ->field('id,cat_id,images,title,introduction,content,createtime')
                ->where(['id'=>$rq_data['id']])
                ->find();

        $model
        ->where(['id'=>$rq_data['id']])
        ->setInc('click');

        if ( $info ) {
            $info['collection_num'] = db('collection')
                                    ->where(['type'=>$collection_arr[$info['cat_id']],'relation_id'=>$info['id']])
                                    ->count();

            $info['collection_status'] = db('collection')
                                        ->where(['type'=>$collection_arr[$info['cat_id']],'relation_id'=>$info['id'],'user_id'=>$this->user_id])
                                        ->count();

            unset($info['cat_id']);
            $info['register_status'] = $srmodel
                                        ->where(['article_id'=>$info['id'],'user_id'=>$this->user_id])
                                        ->count();
        }

        $this->success('請求成功',$info?$info:[]);
    }

    /**
     * @MethodName 加入我們操作收藏
     * @Author     Allen
     * @DateTime   2021-10-04T16:12:36+0800
     * @param      string
     */
    public function setJoinArticleCollection($value='')
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Article');
        if (!$validate->scene('info')->check($rq_data)) {
            $this->error($validate->getError());
        }
        $model = new JoinArticles;
        $info = $model
                ->field('id,cat_id,images,title,content,createtime')
                ->where(['id'=>$rq_data['id']])
                ->find();

        if ( !$info ) {
            $this->error('操作失敗');
        }

        $collection_arr = ['1'=>'7','2'=>'8'];

        $collection_status = db('collection')
                            ->where(['type'=>$collection_arr[$info['cat_id']],'relation_id'=>$rq_data['id'],'user_id'=>$this->user_id])
                            ->count();

        if ( $collection_status ) {
            $status = db('collection')
                    ->where(['type'=>$collection_arr[$info['cat_id']],'relation_id'=>$rq_data['id'],'user_id'=>$this->user_id])
                    ->delete();
        }else{
            $insert_arr = [];
            $insert_arr['user_id'] = $this->user_id;
            $insert_arr['type'] = $collection_arr[$info['cat_id']];
            $insert_arr['relation_id'] = $rq_data['id'];
            $insert_arr['createtime'] = time();
            $status = db('collection')->insert($insert_arr);
        }

        if ( !$status ) {
            $this->error('操作失敗');
        }

        $this->success('操作成功');
    }

    /**
     * @MethodName 報名
     * @Author     Allen
     * @DateTime   2021-10-05T14:58:19+0800
     * @param      string
     * @return     [type]
     */
    public function signup($value='')
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Article');
        if (!$validate->scene('signup')->check($rq_data)) {
            $this->error($validate->getError());
        }
        $model = new JoinArticles;
        $info = $model
                ->field('id,cat_id,images,title,content,createtime')
                ->where(['id'=>$rq_data['id']])
                ->find();

        if ( !$info ) {
            $this->error('操作失敗');
        }
        $srmodel = new Signup;
        $register_status = $srmodel
                            ->where(['article_id'=>$info['id'],'user_id'=>$this->user_id])
                            ->count();
        
        if ( $register_status ) {
            $this->error('請勿重複報名');
        }

        $insert_arr = [];
        $insert_arr['article_id'] = $info['id'];
        $insert_arr['user_id'] = $this->user_id;
        $insert_arr['name'] = $rq_data['name'];
        $insert_arr['sex'] = $rq_data['sex'];
        $insert_arr['id_card'] = $rq_data['id_card'];
        $insert_arr['birthday'] = $rq_data['birthday'];
        $insert_arr['mobile'] = $rq_data['mobile'];
        $insert_arr['createtime'] = time();
        $status = $srmodel->insert($insert_arr);

        if ( !$status ) {
            $this->error('操作失敗');
        }

        $this->success('操作成功');
    }


}